/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.blitzcoder.collide.engine.parser;

import de.blitzcoder.collide.docs.Module;
import de.blitzcoder.collide.util.Log;
import java.io.File;
import java.io.Serializable;

/**
 *
 * @author blitzcoder
 */
public abstract class CodeObject implements Serializable {
    
    
    private String name;
    private File file = null;
    private CodeType parent = null; // Parent type, if existing
    private int line;
    
    private String comment; // BBDoc or other comment
    
    // BBDoc
    private String bbDoc = "";
    private String about = "";
    private String returns = "";
    private Module module = null;
    
    public CodeObject(String name,int line) {
        this.name = name;
        this.line = line;
    }
    
    public abstract String getObjectType();
    
    public int getLine() {
        return line;
    }
    
    public void setModule(Module module) {
        this.module = module;
    }
    
    public Module getModule() {
        return module;
    }

    public String getName() {
        return name;
    }
    
    @Override
    public String toString() {
        return getName();
    }
    
    public void setParentType(CodeType type) {
        this.parent = type;
    }
    
    public CodeType getParentType() {
        return parent;
    }

    public String getComment() {
        return comment;
    }
    
    public void setFile(File file) {
        this.file = file;
    }
    
    public File getFile() {
        return file;
    }

    public void setComment(String comment) {
        this.comment = comment;
        try {
            parseBBDoc();
        } catch (Exception ex) {
            Log.log("Failed to parse BBDoc comment: "+ex.getClass().getName());
        }
    }
    
    public String getBBDoc() {
        return bbDoc;
    }
    
    public String getReturns() {
        return returns;
    }

    public void setLine(int line) {
        this.line = line;
    }
    
    public String getAbout() {
        return about;
    }

    // Check if BBDoc is available and parse if true
    private void parseBBDoc() throws StringIndexOutOfBoundsException {
        
        int bbDocPos    = comment.indexOf("bbdoc:");
        int returnsPos  = comment.indexOf("returns:");
        int aboutPos    = comment.indexOf("about:");
        
        if (bbDocPos == -1)
            bbDocPos = comment.length();
        if (aboutPos == -1)
            aboutPos = comment.length();
        if (returnsPos == -1)
            returnsPos = comment.length();


        if (bbDocPos != comment.length()) 
            if (aboutPos < returnsPos) 
                bbDoc = comment.substring(bbDocPos+6, aboutPos-1);
            else
                bbDoc = comment.substring(bbDocPos+6, returnsPos-1);
        
        if (aboutPos != comment.length())
                about = comment.substring(aboutPos+6,returnsPos-1);
        
        if (returnsPos != comment.length())
                returns = comment.substring(returnsPos+8,comment.length()-1);
        
    }

    public boolean equals(Object o) {
        if (o instanceof CodeObject) {
            return ((CodeObject)o).getName().equals(this.getName());
        } else {
            return false;
        }
    }
    
}
